package com.bawei.module_detail.viewmodel

import androidx.lifecycle.viewModelScope
import com.bawei.lib_common.mvi.base.BaseViewModel
import com.bawei.module_detail.intent.CommentIntent
import com.bawei.module_detail.model.entity.DanmakuParam
import com.bawei.module_detail.model.entity.SendCommentParam
import com.bawei.module_detail.model.repository.DetailRepository
import com.bawei.module_detail.state.CommentState
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.consumeAsFlow
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.launch

class CommentViewModel : BaseViewModel<CommentIntent>() {
    private val repo : DetailRepository by lazy { DetailRepository() }
    private var _commentStateFlow = MutableStateFlow<CommentState>(CommentState.init)
    val commentStateFlow = _commentStateFlow.asStateFlow()

    private var _sendCommentStateFlow = MutableStateFlow<CommentState>(CommentState.init)
    val sendCommentStateFlow = _sendCommentStateFlow.asStateFlow()
    //获取弹幕
    private var _getBulletScreenInfo = MutableStateFlow<CommentState>(CommentState.init)
    val getBulletScreenInfo = _getBulletScreenInfo.asStateFlow()
    //发送弹幕
    private var _sendBulletScreenInfo = MutableStateFlow<CommentState>(CommentState.init)
    val sendBulletScreenInfo = _sendBulletScreenInfo.asStateFlow()



    override fun handleIntent() {
        viewModelScope.launch {
            intents.consumeAsFlow().collect({
                when(it){
                    is CommentIntent.getBulletScreenInfo -> {
                        getBulletScreenInfo(it.type,it.itemId)
                    }
                    is CommentIntent.getComment -> {
                        getComment(it.type,it.itemId)
                    }
                    is CommentIntent.getSendComment -> {
                        SendComment(it.param)
                    }
                    is CommentIntent.getRecommendSimpleVideo -> {
                        getRecommendSimpleVideo(it.page,it.pagesize)
                    }
                    is CommentIntent.getSendBulletScreenInfo -> {
                        getSendBulletScreenInfo(it.param)
                    }
                }
            })
        }
    }

    private fun getComment(type: Int, itemId:String){
        viewModelScope.launch {
            repo.getComment(type,itemId)
                .catch {
                    _commentStateFlow.value = CommentState.onFailed(it.message.toString())
                }.filter {
                    it !=  null
                }.collect{
                    if (it.code == 200){
                        if(it.data.isEmpty()){
                            _commentStateFlow.value = CommentState.Empty
                        }else{
                            _commentStateFlow.value = CommentState.onSuccess(it.data,"put")
                        }
                    }else{
                        _commentStateFlow.value = CommentState.onFailed(it.msg)
                    }
                }

        }
    }
    private fun SendComment(param : SendCommentParam){
        viewModelScope.launch {
            repo.getSendComment(param)
                .catch {
                    _sendCommentStateFlow.value = CommentState.onFailed(it.message.toString())
                }.collect{
                    if (it.code == 200){
                        _sendCommentStateFlow.value = CommentState.onSuccess(it.data,"send")
                    }else{
                        _sendCommentStateFlow.value = CommentState.onFailed(it.msg)
                    }
                }
        }
    }
    private fun getRecommendSimpleVideo(page : Int,pagesize : Int){
        viewModelScope.launch {
            repo.getRecommendSimpleVideo(page,pagesize)
                .catch {
                    _commentStateFlow.value = CommentState.onFailed(it.message.toString())
                }.collect{
                    if (it.code == 200){
                        _commentStateFlow.value = CommentState.onSuccess(it.data,"hot")
                    }else{
                        _commentStateFlow.value = CommentState.onFailed(it.msg)
                    }
                }

        }
    }

    private fun getBulletScreenInfo(page : Int,itemId : String){
        viewModelScope.launch {
            repo.getBulletScreenInfo(page,itemId)
                .catch {
                    _getBulletScreenInfo.value = CommentState.onFailed(it.message.toString())
                }.collect{
                    if (it.code == 200){
                        _getBulletScreenInfo.value = CommentState.onSuccess(it.data,"putBull")
                    }else{
                        _getBulletScreenInfo.value = CommentState.onFailed(it.msg)
                    }
                }
        }
    }

    private fun getSendBulletScreenInfo(param : DanmakuParam){
        viewModelScope.launch {
            repo.getSendBulletScreenInfo(param)
                .catch {
                    _sendBulletScreenInfo.value = CommentState.onFailed(it.message.toString())
                }.collect{
                    if (it.code == 200){
                        _sendBulletScreenInfo.value = CommentState.onSuccess(it.data,"sendBull")
                    }else{
                        _sendBulletScreenInfo.value = CommentState.onFailed(it.msg)
                    }
                }
        }
    }
}